<template>
  <div class="view">
    <OperateC
        :top-level="topLevel"
        :add="addDictData"
        :edit="editDictData"
        :remove="removeDictData"
        :add-show="this.$getButtonAuth('dict:data:add')"
        :remove-show="this.$getButtonAuth('dict:data:del:batch')"
    />
    <DictDataTable
        :top-level="topLevel"
        :edit-show="this.$getButtonAuth('dict:data:edit')"
        :del-show="this.$getButtonAuth('dict:data:del')"
    />
    <DictDataDialog></DictDataDialog>
  </div>
</template>

<script>
import DictDataTable from "@/components/table/DictDataTable";
import DictDataDialog from "@/components/dialog/DictDataDialog";
import OperateC from "@/components/OperateC";
import {deleteDictDataByIds, getDictDatas} from "@/api/dict-data";

export default {
  name: "DictDataView",
  components: {DictDataTable, DictDataDialog, OperateC},
  data() {
    return {
      topLevel: -1
    }
  },
  methods: {
    addDictData() {
      // 打开添加dialog
      var params = {
        dialogTitle: "添加字典数据",
        dialogFormVisible: true
      }
      this.$store.commit("dictData/openDialogForm",params);
    },
    editDictData() {
      var selected = this.$store.state.dictData.multipleSelection;
      if (selected.length < 1) {
        this.$message.warning("你还没有选中数据哦！");
        return;
      }
      if (selected.length > 1) {
        this.$message.warning("一次只能修改一条数据哦！")
        return;
      }
      this.$store.dispatch("dictData/editDictData", selected[0]);
    },
    removeDictData() {
      var selected = this.$store.state.dictData.multipleSelection;
      if (selected.length < 1) {
        this.$message.warning("请先选择要删除的数据哦！");
        return;
      }
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDictDataByIds(selected).then((res) => {
          this.$message.success(res.data.msg);
          // 刷新
          var params = {
            "current": this.$store.state.dictData.currentPage,
            "size": this.$store.state.dictData.pageSize
          };
          getDictDatas(params).then((res) => {
            this.$store.state.dictData.tableData = res.data.data;
            this.$store.state.dictData.total = res.data.total;
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    }
  }
}
</script>

<style scoped>

</style>
